<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/20
 * Time: 16:17
 */

namespace app\api\controller;

use app\api\model\Recommend as RecommendModel;
use app\api\model\SendCircle as SendCircleModel;
use app\api\model\TransErr;
use app\api\model\User as UserModel;
use app\api\service\Push as PushService;
use app\api\service\Task as TaskService;
use app\api\service\User as UserService;
use think\Db;

class Share
{
    //接收分享任务--接收任务进入待结算
    public function shareTask()
    {
        $h = date('H');
        $i = date('i');
        $now = $h.$i-0;
        if($now < 700 || $now >= 2230){
            return json(['errorCode' => 1, 'msg' => '该时间段分享无效,请在7点到22点30之间分享']);
        }
        $user_id = UserService::getUserIdByToken();
        $share_no = input('post.share_no');
        $user = UserModel::get($user_id);
        $is_member= UserService::isMember($user['member_time']);
        if (!$is_member){
            return json(['errorCode' => 0, 'msg' => '不是月会员', 'data' => ['status'=>0,'data'=>['task_time'=>0]]]);
        }
        if (!UserService::checkDailyTaskTime($user)) {
            return json(['errorCode' => 0, 'msg' => '分享次数已满', 'data' => ['status'=>1,'data'=>['level'=>$user['level']]]]); //status =1 任务次数已满
        }
        $info = SendCircleModel::get(['share_no' => $share_no, 'user_id' => $user_id]);
        if ($info) {
            return json(['errorCode' => 0, 'msg' => '重复分享发圈任务', 'data' => ['status'=>0,'data'=>['task_time'=>$user['task_time']]]]);//status =2 发圈失败
        } else {
            //记录
            Db::startTrans();
            try{
                $data = [
                    'user_id' => $user_id,
                    'share_no' => $share_no,
                    'status' => 0,
                    'add_time' => time(),
                ];
                SendCircleModel::create($data);
                //添加预结算
                TaskService::operationWaitMoney($user);
                Db::commit();
                return json(['errorCode' => 0, 'msg' => '处理成功', 'data' => ['status'=>0,'data'=>['task_time'=>$user['task_time']]]]); //status =0 发圈成功
            }catch (\Exception $e){
                // 回滚事务
                Db::rollback();
                //记录异常
                TransErr::add($e);
                return json(['errorCode' => 0, 'msg' => '保存失败', 'data' => ['status'=>2,'data'=>[]]]);//status =2 发圈失败
            }
        }
    }


    //分享任务分红
    /*public function reward($user_id, $share_no)
    {
        $user = UserModel::get($user_id);
        if (!UserService::checkDailyTaskTime($user->toArray())) {
            return;
        }
        $pid = $user['parentid'];
        //完成任务自己奖励
        $user->money = $user['money'] + config('config.任务_完成奖励金额');
        $user->total_money = $user['total_money'] + config('config.任务_完成奖励金额');
        $user->task_time = $user['task_time'] + 1;//每天任务数
        $user->finish_task = $user['finish_task'] + 1; //增加完成任务数
        $user->save();
        //奖励推送
        $user_push_info = [
            'alert' => '完成分享任务【' . $share_no . '】奖励' . config('config.任务_完成奖励金额')
        ];
        PushService::income($user_id, $user_push_info);
        //余额增加日志
        $log_data[] = [
            'title' => '完成分享任务【' . $share_no . '】奖励',
            'user_id' => $user_id,
            'task_id' => $share_no,
            'type' => 1,
            'amount' => config('config.任务_完成奖励金额'),
            'add_time' => time(),
        ];
        if ($pid) {
            $p_user = UserModel::get($pid);
            $p_user->money = $p_user['money'] + config('config.任务_完成一级推荐奖');
            $p_user->total_money = $p_user['total_money'] + config('config.任务_完成一级推荐奖');
            $log_data[] = [
                'title' => $user['nickname'] . '(ID:' . $user_id . ')完成任务【' . $share_no . '】上级奖励',
                'user_id' => $pid,
                'task_id' => $share_no,
                'type' => 1,
                'amount' => config('config.任务_完成一级推荐奖'),
                'add_time' => time(),
            ];
            //奖励推送
            $p_push_info = [
                'alert' => '直推会员' . $user['nickname'] . '(ID:' . $user_id . ')完成分享任务【' . $share_no . '】奖励' . config('config.任务_完成一级推荐奖'),
            ];
            PushService::income($pid, $p_push_info);
            $p_user->save();
        }

        //推广员奖励
        $promoter_id = RecommendModel::getPromoterID($user_id);
        if ($promoter_id) {
            $pro_user = UserModel::get($promoter_id);
            $pro_user->total_money = $pro_user['total_money'] + config('config.任务_完成推广员奖励');
            $pro_user->money = $pro_user['money'] + config('config.任务_完成推广员奖励');
            $log_data[] = [
                'title' => $user['nickname'] . '(ID:' . $user_id . ')完成任务【' . $share_no . '】脉大师奖励',
                'user_id' => $promoter_id,
                'task_id' => $share_no,
                'type' => 1,
                'amount' => config('config.任务_完成推广员奖励'),
                'add_time' => time(),
            ];
            //奖励推送
            $pro_push_info = [
                'alert' => '团队会员' . $user['nickname'] . '(ID:' . $user_id . ')完成分享任务【' . $share_no . '】奖励' . config('config.任务_完成推广员奖励'),
            ];
            PushService::income($pid, $pro_push_info);
            $pro_user->save();
        }
        if (!empty($log_data)) {
            Db::name('money')->insertAll($log_data);
        }
    }*/
}